﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ITOMMS.Entity;

namespace ITOMMS.Web
{
    public class UserLoginProcess
    {
        public const string COOIKE_AUTH_KEY = ".ITOMMS";

        /// <summary>
        /// 为登录用户设置Cooike
        /// </summary>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <param name="userPass"></param>
        /// <param name="createPersistentCookie"></param>
        public static void SetAuthCookie(HttpContext context, int? id, string userName, string userPass, string userType)
        {
            HttpCookie cookie = FormsAuthentication.GetAuthCookie(id.ToString() + "|" + userName + "|" + userPass + "|" + userType, true);
            cookie.Name = COOIKE_AUTH_KEY;

            cookie.HttpOnly = false;
            context.Response.SetCookie(cookie);
        }

        /// <summary>
        /// 登出操作
        /// </summary>
        /// <param name="context"></param>
        public static void SignOut(HttpContext context)
        {
            context.Response.Cookies.Set(new HttpCookie(COOIKE_AUTH_KEY));
        }

        /// <summary>
        /// 判别用户是否登录
        /// </summary>
        /// <param name="context"></param>
        /// <param name="ticket"></param>
        /// <returns></returns>
        private static bool GetIsAnonymousUser(HttpContext context, out FormsAuthenticationTicket ticket)
        {
            string authToken;
            HttpCookie cookie;

            cookie = context.Request.Cookies[COOIKE_AUTH_KEY];

            if (cookie != null)
            {
                authToken = cookie.Value;
            }
            else
            {
                authToken = context.Request.Params["auth"];
            }

            if (authToken == null)
            {
                ticket = null;
                return true;
            }

            try
            {
                ticket = FormsAuthentication.Decrypt(authToken);
                return false;
            }
            catch (Exception)
            {
                ticket = null;
                return true;
            }
        }

        /// <summary>
        /// 从Cooike中还原用户
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static tbUserEntity GetIdentity(HttpContext context)
        {
            FormsAuthenticationTicket ticket;

            if (GetIsAnonymousUser(context, out ticket))
            {
                return null;
            }

            tbUserEntity existingUser = new tbUserEntity();

            if (ticket != null)
            {
                string[] parts = ticket.Name.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

                existingUser.ID = Int32.Parse(parts[0]);
                existingUser.UserName = parts[1];
                existingUser.PWD = parts[2];
                existingUser.RoleGroup = int.Parse(parts[3]);
            }

            return existingUser;
        }

    }
}
